package com.yunkaweilai.android.activity.login;

import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.github.lazylibrary.util.StringUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.login.UserInfoModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 修改密码
 * Created by Administrator on 2017/7/25.
 */

public class UpdatePassActivity extends BaseActivity {

    @BindView(R.id.id_edt_old_pass)
    EditText idEdtOldPass;
    @BindView(R.id.id_edt_new_pass)
    EditText idEdtNewPass;
    @BindView(R.id.id_edt_new_pass_2)
    EditText idEdtNewPass2;
    @BindView(R.id.id_btn_save)
    Button idBtnSave;
    @BindView(R.id.id_check_pass)
    CheckBox idCheckPass;
    @BindView(R.id.id_check_pass2)
    CheckBox idCheckPass2;
    @BindView(R.id.id_check_pass3)
    CheckBox idCheckPass3;
    private UserInfoModel.DataBean.StoreEmployeeInfoBean userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_pass);
        ButterKnife.bind(this);
        userInfo = BaseApplication.getUserInfo();
        new TitleBuilder(mAc).setTitleText("修改密码")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        idBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isRight();
            }
        });

        initClick();
    }

    private void initClick() {
        idCheckPass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    //如果选中，显示密码
                    idEdtOldPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    //否则隐藏密码
                    idEdtOldPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                idEdtOldPass.setSelection(idEdtOldPass.length());
            }
        });

        idCheckPass2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    //如果选中，显示密码
                    idEdtNewPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    //否则隐藏密码
                    idEdtNewPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                idEdtNewPass.setSelection(idEdtNewPass.length());
            }
        });

        idCheckPass3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    //如果选中，显示密码
                    idEdtNewPass2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    //否则隐藏密码
                    idEdtNewPass2.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                idEdtNewPass2.setSelection(idEdtNewPass2.length());
            }
        });
    }

    private void isRight() {
        if (StringUtils.isEmpty(idEdtOldPass.getText().toString())) {
            showToast("还未输入原密码");
            return;
        }
        if (StringUtils.isEmpty(idEdtNewPass.getText().toString())) {
            showToast("还未输入新密码");
            return;
        }
        if (StringUtils.isEmpty(idEdtNewPass2.getText().toString())) {
            showToast("还未输入确认密码");
            return;
        }
        if (!idEdtNewPass.getText().toString().equals(idEdtNewPass2.getText().toString())) {
            showToast("两次密码输入不一致");
            return;
        }
        if (userInfo != null) {
            initHttp();
        } else {
            Utils.RefreshUserInfo();
        }
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.UPDATE_PASSWORD)
                .addParams("employee_id", userInfo.getId())
                .addParams("y_password", idEdtOldPass.getText().toString())
                .addParams("n_password", idEdtNewPass.getText().toString())
                .addParams("n_passwords", idEdtNewPass2.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            showToast("密码修改成功");
                            finish();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        showToast(R.string.http_error);
                    }
                });

    }
}
